package com.servlets;

import com.alibaba.fastjson.JSON;
import com.pojo.ReceivingAddress;
import com.service.ReceivingAddressService;
import com.service.impl.ReceivingAddressServiceImpl;
import com.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/receivingAddress.do")
public class ReceivingAddressServlet extends BaseServlet{
    private ReceivingAddressService service = new ReceivingAddressServiceImpl();

    public void selectByUserID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String userID = request.getParameter("userID");
        List<ReceivingAddress> list = service.selectByUserID(userID);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(list));
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, String[]> map = request.getParameterMap();
        response.setContentType("application/json;charset=utf-8");
        ReceivingAddress receivingAddress = JDBCUtil.mapToBean(map, ReceivingAddress.class);
        receivingAddress.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        String msg;
        if (service.selectByUserID(receivingAddress.getUserId()).size()>5){
            msg = "最多只能保存5个地址";
        }else{
            if(service.addRA(receivingAddress))
                msg = "添加成功";
            else
                msg = "添加失败";
        }


        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, String[]> map = request.getParameterMap();
        ReceivingAddress receivingAddress = JDBCUtil.mapToBean(map, ReceivingAddress.class);
        String msg;
        if(service.updateRA(receivingAddress))
            msg = "修改成功";
        else
            msg = "修改失败";
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        String msg;
        if(service.deleteRA(id))
            msg = "删除成功";
        else
            msg = "删除失败";
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void findByID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        ReceivingAddress receivingAddress=null;
        if (id!=null&&id.length()>0){
            receivingAddress = service.findByID(id);
        }
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(receivingAddress));
    }

}
